package com.example.musicplayer

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MusicList:Fragment() {

    private val musicData=ArrayList<music>()

    private lateinit var mediaPlayer:MediaPlayer

    private lateinit var musicList:RecyclerView

    private lateinit var musicBottomImage:ImageView

    private lateinit var musicBottomTitle:TextView

    private lateinit var musicBottomSinger:TextView

    private lateinit var changeImage:ImageButton

    private lateinit var stopImage:ImageButton


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view=inflater.inflate(R.layout.music_list,container,false)
        musicList=view.findViewById(R.id.musicList)
        musicBottomImage=view.findViewById(R.id.musicBottomImage)
        musicBottomTitle=view.findViewById(R.id.musicBottomTitle)
        musicBottomSinger=view.findViewById(R.id.musicBottomSinger)
        changeImage=view.findViewById(R.id.changeImage)
        stopImage=view.findViewById(R.id.stopImage)
        mediaPlayer=MediaPlayer()
        return view
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val JSONWithMusicListService=ServiceCreator.create(JSONImageService::class.java)
        JSONWithMusicListService.getListData().enqueue(object : Callback<List<music>> {
            override fun onResponse(call: Call<List<music>>, response: Response<List<music>>) {
                val list=response.body()
                if (list!=null) {
                    for (i in list) {
                        musicData.add(i)
                    }
                }
                val layoutManager=LinearLayoutManager(context)
                musicList.layoutManager=layoutManager
                val adapter=MusicListAdapter(musicData) { music -> playMusic(music) }
                musicList.adapter=adapter
            }

            override fun onFailure(p0: Call<List<music>>, p1: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }

    //设置底部音乐播放器
    private fun playMusic(music: music?) {
        if (music==null) {
            return
        }
        else {
            Glide.with(this@MusicList)
                .load(music.coverImgUrl)
                .into(musicBottomImage)
            musicBottomTitle.text=music.title
            musicBottomSinger.text=music.singer
            changeImage.setImageResource(R.drawable.play)
            stopImage.setImageResource(R.drawable.replay)
            initMediaPlayer(music.src)
            setupChangeImageClickListener()
            setupStopImageClickListener(music.src)
        }

    }
    private fun initMediaPlayer(musicUrl:String) {
        mediaPlayer.release()
        mediaPlayer=MediaPlayer.create(requireContext(), Uri.parse(musicUrl))
        changeImage.setImageResource(R.drawable.play)
    }

    private fun setupChangeImageClickListener() {
        changeImage.setOnClickListener {
            if (!mediaPlayer.isPlaying) {
                mediaPlayer.start()
                changeImage.setImageResource(R.drawable.pause)
            }
            else {
                mediaPlayer.pause()
                changeImage.setImageResource(R.drawable.play)
            }
        }
    }

    private fun setupStopImageClickListener(musicUrl:String) {
        stopImage.setOnClickListener {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.release()
                initMediaPlayer(musicUrl)
            }
            else if (!mediaPlayer.isPlaying) {
                mediaPlayer.release()
                initMediaPlayer(musicUrl)
            }
        }
    }
}